package analyzemytime.anlyzemytime;

import java.util.ArrayList;
import java.util.HashMap;

import analyzemytime.datamodel.*;
import analyzemytime.reports.MonthlyEmployeeReport;
import analyzemytime.reports.MonthlyOverallReport;
import analyzemytime.reports.MonthlyProjectReport;

/**
 * Hello world!
 * jenkins test
 */
public class App 
{
    public static void main( String[] args )
    {
        CommandLine cm = new CommandLine(args);
        
        if(!cm.getPeriod_month().isEmpty() && !cm.getPeriod_year().isEmpty())
        {
            ReportAggregator reportAggregator = new ReportAggregator(cm.getPeriod_month(),cm.getPeriod_year());
            ArrayList<Report> reports = reportAggregator.getMonthlyReport();
            HashMap<Project, ArrayList<Report>> map = reportAggregator.segregateByProject(reports);
            HashMap<Employee, ArrayList<Report>> map2 = reportAggregator.segregateByEmployee(reports);
            
            if(cm.getFirstName() == null && cm.getLastName() == null && cm.getProjectName() == null && cm.getPeriod_month() != null && cm.getPeriod_year() != null && cm.getType()!=null)
            {  
    	        if(cm.getType().equals("employee")){
    	        	MonthlyEmployeeReport monthlyEmployeeReport = new MonthlyEmployeeReport(map2);
    		        monthlyEmployeeReport.generateMonthlyEmployeeOverallReport(cm.getPeriod_month(), cm.getPeriod_year());
    		        MonthlyOverallReport overallReport = new MonthlyOverallReport(monthlyEmployeeReport.getMonthlyEmployeeOverallReportMap(), cm.getPeriod_month(), cm.getPeriod_year());
    	        	overallReport.printMaps("employee");
    	        }else if(cm.getType().equals("project")){
    	        	MonthlyProjectReport monthlyProjectReport = new MonthlyProjectReport(map);
    	        	monthlyProjectReport.generateMonthlyProjectOverallReport(cm.getPeriod_month(), cm.getPeriod_year());;
    		        MonthlyOverallReport overallReport = new MonthlyOverallReport(monthlyProjectReport.getMonthlyEmployeeOverallReportMap(), cm.getPeriod_month(), cm.getPeriod_year(), ""); 
    	        	overallReport.printMaps("project");
    	        }else{
    	        	System.out.println("Unknown type. Provide type=project/employee");
    	        }
            }

            if(cm.getFirstName() != null && cm.getLastName() != null && cm.getPeriod_month() != null && cm.getPeriod_year() != null)
            {
                MonthlyEmployeeReport monthlyEmployeeReport = new MonthlyEmployeeReport(map2);
                monthlyEmployeeReport.generateMonthlyEmployeeReport(cm.getFirstName(),cm.getLastName(),cm.getPeriod_month(),cm.getPeriod_year());
                monthlyEmployeeReport.printMonthlyEmployeeReportToConsole(cm.getFirstName(),cm.getLastName(),cm.getPeriod_month(),cm.getPeriod_year());
                if(cm.saveReport == true)
                {
                	System.out.println("Saving monthly employee report for "+cm.getFirstName()+" "+cm.getLastName()+" year "+cm.getPeriod_year()+" month "+cm.getPeriod_month());
                	monthlyEmployeeReport.printMonthlyEmployeeReportToFile(cm.getFirstName(),cm.getLastName(),cm.getPeriod_month(),cm.getPeriod_year());
                }  
            }
            
            if(cm.getProjectName() != null && cm.getPeriod_month() != null && cm.getPeriod_year() != null)
            {
                MonthlyProjectReport monthlyProjectReport= new MonthlyProjectReport(map);
                monthlyProjectReport.generateMonthlyProjectReport(cm.getProjectName(),cm.getPeriod_month(),cm.getPeriod_year());
                monthlyProjectReport.printMonthlyProjectReportToConsole(cm.getProjectName(),cm.getPeriod_month(),cm.getPeriod_year());
                
                if(cm.saveReport == true)
                {
                	System.out.println("Saving monthly project report for "+cm.getProjectName()+" year "+cm.getPeriod_year()+" month "+cm.getPeriod_month());
                	monthlyProjectReport.printMonthlyProjectReportToFile(cm.getProjectName(),cm.getPeriod_month(),cm.getPeriod_year());
                } 
            }
        }

        
    }
}
